package com.weixin1.dao;

import com.weixin1.bean.Case;
import com.weixin1.utils.C3p0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 对案例的操作类
 */
public class CaseDao {
    private QueryRunner qr=new QueryRunner(C3p0Utils.comboPooledDataSource);

    //添加案例,更新案例
    public int save(Case ca) throws SQLException {
        return qr.update("insert into cases(case_id,case_name,case_style,case_color,case_pop," +
                "case_price,case_location,case_intro) values(?,?,?,?,?,?,?,?)",ca.getCaseId(),
                ca.getCaseName(),ca.getCaseStyle(),ca.getCaseColor(),ca.getCasePop(),ca.getCasePrice(),
                ca.getCaseLocation(),ca.getCaseIntro());
    }

    //删除案例
    public int removeById(int id) throws SQLException {
        return qr.update("delete from cases where case_id=?",id);
    }

    //查询全部案例
    public List<Case> list() throws SQLException {
        return qr.query("select case_id caseId,case_name caseName,case_style caseStyle," +
                "case_color caseColor,case_pop casePop,case_price casePrice,case_location " +
                "caseLocation,case_intro caseIntro from cases",new BeanListHandler<Case>(Case.class));
    }

    //根据id查询单个案例
    public Case getById(int id) throws SQLException {
        return qr.query("select case_id caseId,case_name caseName,case_style caseStyle," +
                "case_color caseColor,case_pop casePop,case_price casePrice,case_location " +
                "caseLocation,case_intro caseIntro from cases where case_id=?",new BeanHandler<Case>(Case.class),id);
    }

    //修改案例内容
    public int update(Case ca) throws SQLException {
        return  qr.update("update cases set case_name=?,case_style=?,case_color=?," +
                "case_pop=?,case_price=?,case_location=?,case_intro=? where case_id=?",ca.getCaseName(),
                ca.getCaseStyle(),ca.getCaseColor(),ca.getCasePop(),ca.getCasePrice(),ca.getCaseLocation(),
                ca.getCaseIntro(),ca.getCaseId());
    }

}
